//
//  HDPlaceholderView.m
//  HDUIKit
//
//  Created by VanJay on 2020/3/31.
//  Copyright © 2020 chaos network technology. All rights reserved.
//

#import "HDPlaceholderView.h"


@interface HDPlaceholderView ()
@property (nonatomic, strong) UILabel *placeholderLabel;       ///< 占位文字
@property (nonatomic, strong) UIImageView *placeholderImageV;  ///< 占位图片
@property (nonatomic, strong) BaseLabel *refreshBtn;          ///< 刷新按钮
@property (nonatomic, strong) UIView *stackView;               ///< 控制整体垂直居中
@end

@implementation HDPlaceholderView

#pragma mark - life cycle
- (void)commonInit {
    if (self.model) {
        self.backgroundColor = self.model.backgroundColor;
        if (!self.placeholderImageV) {
            self.placeholderImageV = [[UIImageView alloc] init];
            [self addSubview:self.placeholderImageV];
        }
        if (self.model.image && self.model.image.length) {
            self.placeholderImageV.image = [UIImage imageNamed:self.model.image];
        }

        if (!self.placeholderLabel) {
            self.placeholderLabel = [[UILabel alloc] init];
            self.placeholderLabel.numberOfLines = 0;
            self.placeholderLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.placeholderLabel];
        }

        self.placeholderLabel.text = self.model.title;
        self.placeholderLabel.textColor = self.model.titleColor;
        self.placeholderLabel.font = self.model.titleFont;

        if (!self.refreshBtn) {
            self.refreshBtn = BaseLabel.new;
            self.refreshBtn.numberOfLines = 0;
            self.refreshBtn.userInteractionEnabled = YES;
            [self.refreshBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRefreshBtn)]];
            [self addSubview:self.refreshBtn];
        }
        self.refreshBtn.hd_edgeInsets = self.model.refreshButtonLabelEdgeInset;
        if (self.model.refreshBtnAttributeTitle.length) {
            self.refreshBtn.attributedText = self.model.refreshBtnAttributeTitle;
        } else {
            self.refreshBtn.text = self.model.refreshBtnTitle;
            self.refreshBtn.textColor = self.model.refreshBtnTitleColor;
            self.refreshBtn.font = self.model.refreshBtnTitleFont;
        }
        self.refreshBtn.layer.backgroundColor = self.model.refreshBtnBackgroundColor.CGColor;
        self.refreshBtn.layer.cornerRadius = (self.model.refreshButtonLabelEdgeInset.top + self.model.refreshButtonLabelEdgeInset.bottom + ceilf([self.model.refreshBtnTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.model.refreshBtnTitleFont} context:nil].size.height))/2;
        self.refreshBtn.hidden = !self.model.needRefreshBtn;

        if (!self.stackView) {
            self.stackView = [[UIView alloc] init];
            [self addSubview:self.stackView];
        }

        [self bringSubviewToFront:self];
        [self bringSubviewToFront:self.placeholderImageV];
        [self bringSubviewToFront:self.placeholderLabel];
        [self bringSubviewToFront:self.refreshBtn];
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)updateConstraints {
    [super updateConstraints];

    if (self.placeholderImageV && !self.placeholderImageV.isHidden) {
        [self.placeholderImageV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            if (self.model.scaleImage) {
                make.size.mas_equalTo(self.model.imageSize);
            } else {
                make.size.mas_equalTo(self.placeholderImageV.image.size);
            }
        }];
    }

    if (self.placeholderLabel && !self.placeholderLabel.isHidden) {
        [self.placeholderLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.equalTo(self).offset(-2 * PostNum(15));
            make.top.equalTo(self.placeholderImageV.mas_bottom).offset(self.model.marginInfoToImage);
        }];
    }

    if (self.refreshBtn && !self.refreshBtn.isHidden) {
        [self.refreshBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.placeholderLabel.mas_bottom).offset(self.model.marginBtnToInfo);
        }];
    }

    if (self.stackView && !self.stackView.isHidden) {
        [self.stackView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.placeholderImageV);
            if (self.refreshBtn.isHidden) {
                make.bottom.equalTo(self.placeholderLabel);
            } else {
                make.bottom.equalTo(self.refreshBtn);
            }
            make.centerY.equalTo(self);
            make.left.lessThanOrEqualTo(self.placeholderImageV);
            make.left.lessThanOrEqualTo(self.placeholderLabel);
            make.right.greaterThanOrEqualTo(self.placeholderImageV);
            make.right.greaterThanOrEqualTo(self.placeholderLabel);
        }];
    }
}

#pragma mark - override system methods
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.model.needRefreshBtn) {
        if (CGRectContainsPoint(self.refreshBtn.frame, point)) {
            return YES;
        } else {
            return NO;
        }
    } else {
        return NO;
    }
    return [super pointInside:point withEvent:event];
}

#pragma mark - event response
- (void)tappedRefreshBtn {
    
    if (self.model.clickOnRefreshButtonHandler) {
        self.model.clickOnRefreshButtonHandler();
    } else {
        !self.tappedRefreshBtnHandler ?: self.tappedRefreshBtnHandler();
    }
}

#pragma mark - getters and setters

- (void)setModel:(UIViewPlaceholderViewModel *)model {
    _model = model;

    [self commonInit];

    [self setNeedsUpdateConstraints];
}
@end
